<?php

/**
 * ClassName:评论操作器
 * @auditor LeiJunXiang
 * @time 2024/11/15
 */

namespace app\modelOpreate\invite;

use app\modelOpreate\ModelOpeBase;
use app\modelOpreate\user\ForumUserOpe;
use Exception;
use think\facade\Db;
use think\facade\Log;

class CommentOpe extends ModelOpeBase
{
    const TOOL_COMMENT_OPE = "CommentOpe";

    /**
     * 更新评论是否包含子评论(评论key)
     * @return void
     */
    public function updateIsChildComment($objKey)
    {
        $flag = Db::table($this->table)
            ->where('objKey', $objKey)
            ->update(['isChildComment' => 1]);
        return $flag;
    }

    /**
     * 查询评论(对象key,起始位置，条数)
     * @param $fatherObjKey
     * @param $showChildCommentNum
     * @return array
     */
    public function selectComment($fatherObjKey, $offset = 0, $limit = 10)
    {
        $commens = Db::table($this->table)
            ->alias('c')
            ->join('ForumUser u', 'c.commentUserId = u.userId')
            ->where('fatherObjKey', $fatherObjKey)
            ->where('isPublish', 1)
            ->limit($offset, $limit)
            ->order('commentTime', 'desc')
            ->field('u.userId,u.userIcon,u.userName,u.userLevel,c.*')
            ->select()
            ->toArray();
        return $commens;
    }


    /**
     * 查询子评论总数
     * @return void
     */
    public function selectCommentCount($objKey)
    {
        if (empty($objKey)) {
            return;
        }
        $count = Db::table($this->table)
            ->where('fatherObjKey', $objKey)
            ->count();
        return $count;
    }

    /**
     * 删除评论(用户id,评论对象key)
     */
    public function delComment($userId, $objKey)
    {
        $arr = ['objKey' => $objKey,
            'commentUserId' => $userId];
        $flag = Db::table($this->table)
            ->where($arr)
            ->update(['isDelete' => 1]);
        return $flag;
    }

    /**
     * 点赞评论(对象key)
     */
    public function likeComment($objKey)
    {
        $flag = Db::table($this->table)
            ->where('objKey', $objKey)
            ->inc('like')
            ->update();
        return $flag;
    }


}